package blog.service.impl;

import blog.entity.Tag;
import blog.mapper.ArticleMapper;
import blog.mapper.TagMapper;
import blog.service.TagService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {
    @Resource
    private TagMapper tagMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Override
    public List<Tag> findAllTag() {
        List<Tag> tagList = tagMapper.findAllTags();
        //添加每个标签所对应的文章数
        for (Tag tag:tagList){
            int articleCount = articleMapper.getArticleCountOfTag(tag.getTagId());
            tag.setTagArticleCount(articleCount);
        }
        return tagList;
    }

    @Override
    public void deleteTagById(Integer id) {
        tagMapper.deleteTagById(id);
    }

    @Override
    public void addTag(String tagName, String tagDescription) {
        tagMapper.insertTag(tagName,tagDescription);
    }

    @Override
    public Tag selectTagByName(String name) {
        return tagMapper.selectTagByName(name);
    }

    @Override
    public List<Tag> getArticleTag(Integer id) {
        return tagMapper.selectTagsByArticleId(id);
    }

    @Override
    public Tag getTagById(Integer id) {
        return tagMapper.getTagById(id);
    }

    @Override
    public void updateTag(Tag tag) {
        tagMapper.updateTag(tag);
    }
}
